<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="理财名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入理财名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="周期" prop="cycle">
        <el-input
          v-model="queryParams.cycle"
          placeholder="请输入周期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户等级" prop="userLevelId">
        <el-input
          v-model="queryParams.userLevelId"
          placeholder="请输入用户等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户等级" prop="userLevel">
        <el-input
          v-model="queryParams.userLevel"
          placeholder="请输入用户等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="余额区间" prop="amountMin">
        <el-input
          v-model="queryParams.amountMin"
          placeholder="请输入余额区间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="余额区间" prop="amountMax">
        <el-input
          v-model="queryParams.amountMax"
          placeholder="请输入余额区间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="收益百分比" prop="income">
        <el-input v-model="queryParams.income" placeholder="请输入收益百分比" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="明细数量" prop="detailCount">
        <el-input
          v-model="queryParams.detailCount"
          placeholder="请输入明细数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否删除" prop="deleted">
        <el-input
          v-model="queryParams.deleted"
          placeholder="请输入是否删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['car:offline:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['car:offline:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['car:offline:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['car:offline:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getOfflineList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="offlineList">
      <el-table-column label="理财名称" align="left" prop="name" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="理财详情" align="left" prop="detail" width="300" :show-overflow-tooltip="true" />
      <el-table-column label="是否上架" align="center" prop="status" width="120">
        <template slot-scope="scope">
          <!-- <dict-tag :options="dict.type.financial_status" :value="scope.row.status" /> -->
          <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
            :inactive-value="2" @change="statusChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="周期" align="center" prop="cycle" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="用户等级" align="center" prop="userLevel" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="最低余额要求" align="center" prop="amountMin" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ formatter(scope.row, 'amountMin') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最高余额要求" align="center" prop="amountMax" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ formatter(scope.row, 'amountMax') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收益奖励" align="center" prop="income" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope"> {{ scope.row.income }} </template>
      </el-table-column>
      <!-- <el-table-column label="是否删除" align="center" prop="deleted" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <div>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['car:offline:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['car:offline:remove']">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getOfflineList" />

    <!-- 添加或修改线下理财对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="理财名称" prop="name">
              <el-input class="modal-input" v-model="form.name" placeholder="请输入理财名称" />
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="理财介绍" prop="detail">
              <el-input class="modal-input" v-model="form.detail" type="textarea" placeholder="请输入内容" />
            </el-form-item>

          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="周期" prop="cycle">
              <el-input-number :controls="true" :min="1" class="modal-input" v-model="form.cycle"
              :step="1" @blur="changeCycle"
                placeholder="请输入周期(天)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户等级" prop="userLevel">
              <!-- <el-input v-model="form.userLevelId" placeholder="请输入用户等级" /> -->
              <el-select class="modal-input" v-model="form.userLevel" @change="userLevelChange">
                <el-option v-for="(item) in userLevelList" :key="item.id" :label="item.name"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="最低余额要求" prop="amountMin">
              <el-input-number :controls="true" :min="0" class="modal-input" v-model="form.amountMin"
                placeholder="请输入最低余额要求" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最高余额要求" prop="amountMax">
              <el-input-number :controls="true" :min="0" class="modal-input" v-model="form.amountMax"
                placeholder="请输入最高余额要求" />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="明细数量" prop="detailCount">
              <el-input-number :controls="true" :min="0" class="modal-input" v-model="form.detailCount"
                placeholder="请输入明细数量" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="收益奖励" prop="income">
               <el-input-number class="modal-input" :controls="true" :min="0" v-model="form.income" placeholder="请输入收益奖励" />
<!--              <percentage :controls="true" :num.sync="form.income"></percentage>-->
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="喜欢数" prop="likeCount">
          <el-input-number :controls="true" :min="0" class="modal-input" v-model="form.likeCount"
                        @blur="likeCount"   placeholder="请输入喜欢数" />
        </el-form-item>
        <el-form-item label="鲜花数" prop="flowerCount">
          <el-input-number :controls="true" :min="0" class="modal-input" v-model="form.flowerCount"
                         @blur="flowerCount"  placeholder="请输入鲜花数" />
        </el-form-item>
        <el-form-item label="点赞数" prop="likesCount">
          <el-input-number :controls="true" :min="0" class="modal-input" v-model="form.likesCount"
                        @blur="likesCount"   placeholder="请输入点赞数" />
        </el-form-item>
        <el-row>
          <el-col :span="24">
            <el-form-item label="任务奖励配置" prop="detailList">
              <OfflineItems :list="addVodieList" @tableChange="tableChange"></OfflineItems>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOffline, changeStatus, getOffline, delOffline, addOffline, updateOffline } from "@/api/financial/offlineLlist";
import { userLevelList } from "@/api/setting/gradeRebate";
import OfflineItems from "@/views/financial/components/OfflineItems";
export default {
  name: "OfflineList",
  components: {
    OfflineItems,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线下理财表格数据
      offlineList: [],
      //用户等级列表
      userLevelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: {
          required: true,
          message: '请输入理财产品名称',
          trigger: ["blur"],
        },
        detail: {
          required: true,
          message: '请输入理财产品介绍',
          trigger: ["blur"],
        },
        cycle: {
          required: true,
          message: '请输入理财周期',
          trigger: ["blur"],
        },
        userLevel: {
          required: true,
          message: '请选择用户等级',
          trigger: ["blur"],
        },
        amountMin: {
          required: true,
          message: '请输入最低余额要求',
          trigger: ["blur"],
        },
        amountMax: {
          required: true,
          message: '请输入最高余额要求',
          trigger: ["blur"],
        },
        income: {
          required: true,
          message: '请输入收益奖励',
          trigger: ["blur"],
        },
        detailCount: {
          required: true,
          message: '请输入明细数量',
          trigger: ["blur"],
        },
        likeCount: {
          required: true,
          message: '请输入喜欢数',
          trigger: ["blur"],
        },
        flowerCount: {
          required: true,
          message: '请输入鲜花数',
          trigger: ["blur"],
        },
        likesCount: {
          required: true,
          message: '请输入点赞数',
          trigger: ["blur"],
        },
      },
      addVodieList: [],
      //表单提交类型，0 添加   1  更新
      type: null,
    };
  },
  created() {
    this.getOfflineList();
    this.getUserLevelList();
  },
  methods: {
    /** 查询用户等级列表 */
    getUserLevelList() {
      userLevelList().then(response => {
        this.userLevelList = response.data;
      });
    },
    /** 查询线下理财列表 */
    getOfflineList() {
      this.loading = true;
      listOffline(this.queryParams).then(response => {
        this.offlineList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        detail: null,
        status: null,
        cycle: null,
        userLevelId: null,
        userLevel: null,
        amountMin: null,
        amountMax: null,
        income: null,
        detailCount: null,
        createTime: null,
        creator: null,
        deleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getOfflineList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addVodieList = [];
      this.open = true;
      this.title = "添加线下理财";
      this.type = 0;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.addVodieList = [];
      const id = row.id || this.ids
      getOffline(id).then(response => {
        if(response.success){
          this.form = response.data;
          this.addVodieList = response.data.detailList;
          console.log(this.addVodieList);
          this.title = "修改线下理财";
          this.open = true;
          this.type = 1;

        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let formPop = this.$deepClone(this.form)
          if (this.checkVodieList()) {
            formPop.detailList = this.addVodieList;
            if (formPop.amountMax < formPop.amountMin) {
              this.$message.error("最低限制比最高限制高");
              return;
            }
            // formPop.income = formPop.income / 100;
            if (this.type == 0) {
              addOffline(formPop).then((res) => {
                if (res.success) {
                  this.loading = false;
                  this.open = false;
                  this.getOfflineList();
                  this.$modal.msgSuccess("新增成功");
                } else {
                  this.$message.error(res.message);
                }
              });
            } else {
              updateOffline(formPop).then((res) => {
                if (res.success) {
                  this.loading = false;
                  this.open = false;
                  this.getOfflineList();
                  this.$modal.msgSuccess("修改成功");
                } else {
                  this.$message.error(res.message);
                }
              });
            }
          }
        } else {
          return;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除线下理财编号为"' + ids + '"的数据项？').then(function () {
        return delOffline(ids);
      }).then(() => {
        this.getOfflineList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 修改状态 */
    statusChange(row) {
      changeStatus(row.id).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getOfflineList();
      });
    },
    formatter: function (row, key) {
      return this.$yun.priceFormatter(row[key]);
    },
    tableChange(tableList) {
      this.addVodieList = tableList;
    },
    checkVodieList() {
      if (this.addVodieList.length === 0) {
        this.$message({
          message: "任务奖励配置项目数量不能为0",
          type: "error",
        });
        return false;
      }
      for (let index in this.addVodieList) {
        const item = this.addVodieList[index];
        if (!item.time) {
          this.$message({
            message: "第" + index + "行配置项目不完整，请确认",
            type: "error",
          });
          return false;
        }
      }
      return true;
    },
    changeCycle(e){
      let wholeNum = Math.round(Number(e.target.ariaValueNow))
      this.form.cycle = wholeNum;
    },
    likeCount(e){
      let likeCount = Math.round(Number(e.target.ariaValueNow))
      this.form.likeCount = likeCount;
    },
    flowerCount(e){
      let flowerCount = Math.round(Number(e.target.ariaValueNow))
      this.form.flowerCount = flowerCount;
    },
    likesCount(e){
      let likesCount = Math.round(Number(e.target.ariaValueNow))
      this.form.likesCount = likesCount;
    },
    userLevelChange(e) {
      console.log(e)
      this.form.userLevelId = e;
    },
  }
};
</script>
